package LinkedStack;

import ArrayStack.MyStackEmpty;

public class MyStack implements IStack{
    static class ListNode{
        public int val;
        public ListNode next;


        public ListNode(int val){
            this.val=val;
        }
    }
    //头节点
    public ListNode head;


    @Override
    public void push(int x) {
        ListNode newH=new ListNode(x);
        if(head!=null){
            newH.next=head;
        }
        head=newH;
    }

    @Override
    public int pop() {
        if(head==null){
            throw new MyStackEmpty("出栈时栈内为空");
        }
        int ret=head.val;
        head=head.next;
        return ret;

    }

    @Override
    public int peek() {
        if(head==null){
            throw new MyStackEmpty("出栈时栈内为空");
        }
        int ret=head.val;
        return ret;
    }

    @Override
    public int size() {
        ListNode cur=head;
        int count=0;
        while (cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }

    @Override
    public boolean empty() {
        if (head == null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean full() {
        return false;
    }
}
